﻿$(document).on("pageInit", "#create-cource-score", function (e, id, page) {
    loadCourceList();


    $(page).find("[name=create-exam-form]").on("submit", function (evt) {
        evt.preventDefault();
        var v = true;
        var students = [];
        $('[data-stuid]').each(function (rIndex, rItem) {
            if ($(rItem).find('[name=Score]').val() == "") {
                $.toast("分数均不能为空！");
                v = false;
            }
            students.push({ UserId: rItem.dataset.stuid, Score: $(rItem).find('[name=Score]').val() });
        });
        if (!v) return false;
        utils.$postJson({
            CourceExamEntity: {
                CourceId: $(page).find('[name=CourceId]').val(),
                ClazzId: $(page).find('[name=ClazzId]').val(),
                Title: ""
            }, CourceExamScoreList: students
        }, "/CourceScore/SaveExam", function (response) {
            if (response.indexOf("err:") > -1) {
                $.toast(response.substr(4));
                return false;
            }
            $.toast("保存成功！");
            history.back();
        });
    });

    // loadCourceList();
    function loadCourceList() {
        $.getJSON("/CourceScore/GetCourceList", {}, function (response) {
            var $CourceId = $(page).find('[name="CourceId"]');
            $CourceId.empty();
            if (response.length > 0) {
                $.each(response, function (rIndex, rItem) {
                    $CourceId.append("<option value='" + rItem.CourceId + "'>" + rItem.CourceName + "</option>");
                });
                loadClazzList(response[0].CourceId);
            }
        });
    }
    $(page).find('[name="CourceId"]').on("change", function (evt) {
        loadClazzList($(evt.currentTarget).val());
    });
    function loadClazzList(courceId) {
        $.getJSON("/CourceScore/GetClazzList", { courceId: courceId }, function (response) {
            var $ClazzId = $(page).find('[name="ClazzId"]');
            $ClazzId.empty();
            if (response.length > 0) {
                $.each(response, function (rIndex, rItem) {
                    $ClazzId.append("<option value='" + rItem.ClazzId + "'>" + rItem.ClazzName + "</option>");
                });
                loadStudent(response[0].ClazzId);
            }
        });
    }

    function loadStudent(clazzId) {
        $.getJSON("/CourceScore/GetStuList", { clazzId: clazzId }, function (response) {
            var $stuList = $(page).find('#stu-list');
            $stuList.empty();

            $.each(response, function (rIndex, rItem) {
                $stuList.append('<li class="item-content" data-stuid="' + rItem.UserId + '">\
                    <div class="item-media"><i class="icon icon-f7"></i></div>\
                    <div class="item-inner">\
                        <div class="item-title label">' + rItem.RealName + '</div>\
                        <div class="item-input">\
                            <input type="number" name="Score" max="100" min="0"/>\
                        </div>\
                    </div>\
                </li>');
            });
        });
    }

});